#pragma once
#include "Controls/ContentControl.h"
namespace cxgui
{
using namespace std;


cinder::Vec2f ContentControl::GetPreferedSize()
{	
	if(_content) 
	{
		
		auto size = _content->GetPreferedSize();
		size.x += _padding.x1 + _padding.x2;
		size.y += _padding.y1 + _padding.y2;
		return size;
	}
	return Control::GetPreferedSize();
}

void ContentControl::Draw()
{	
	DrawBackground();
	DrawBorder();
	if(_content) 
	{		
		_content->Draw();
	}
}

shared_ptr<Control> ContentControl::GetContent() const
{
	return _content;
}

void ContentControl::SetContent( shared_ptr<Control> content )
{
	_content = content;
	OnContentChanged();
}

ContentControl::~ContentControl()
{

}

void ContentControl::OnContentChanged()
{
}

cxgui::VerticalAlignment ContentControl::GetVerticalContentAlignment() const
{
	if(_content)
	{
		return _content->GetVerticalAlignment();
	}
	return VerticalAlignment::Center;
}

void ContentControl::SetVerticalContentAlignment( VerticalAlignment alignment )
{
	if(_content)
	{
		_content->SetVerticalAlignment(alignment);
	}	
}

cxgui::HorizontalAlignment ContentControl::GetHorizontalContentAlignment() const
{
	if(_content)
	{
		return _content->GetHorizontalAlignment();
	}
	return HorizontalAlignment::Center;
}

void ContentControl::SetHorizontalContentAlignment( HorizontalAlignment alignment )
{
	if(_content)
	{
		_content->SetHorizontalAlignment(alignment);
	}	
}



void ContentControl::OnClientRectangleChanged()
{
	if(_content)
	{
		auto contentRect = _clientRectangle;
		ShrinkRectangle(contentRect, _padding);
		_content->SetClientRectangle(contentRect);
	}
	IsLayoutDirty(false);
}

void ContentControl::Update()
{
	if(_content)
	{
		if(_content->IsLayoutDirty())
		{
			auto contentArea = _clientRectangle;
			ShrinkRectangle(contentArea, _padding);
			_content->SetClientRectangle(contentArea);
			IsLayoutDirty(false);
		}
		_content->Update();
	}
}

bool ContentControl::IsLayoutDirty() const
{
	
	if(_content && !_isLayoutDirty)
	{
		return _content->IsLayoutDirty();
	}
	return _isLayoutDirty;
}

void ContentControl::IsLayoutDirty( bool isDirty )
{
	Control::IsLayoutDirty(isDirty);
}



}